Repository navigation
Image Editor: Add mapImageEdit to apply an edit's parts in order - #86704
Conversation
🤖 PR meta 🤖🎉 PropsIf you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
Updated as activity occurs, without notifying anyone named here. Add the 📦 Bundle sizeSize Change: +36 B (0%) Total Size: 8.66 MB 📦 View Changed
⚡ PerformanceShow the resultsClient side metrics exclude the server response time. front-end-block-theme
front-end-classic-theme
media-processing
media-upload
post-editor
site-editor
🏁 Flaky testsSome tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information. Undo restores the post selection when another synced entity is loaded (RTC WebSocket) in
|
| */ | ||
| export function mapImageEdit< T >( | ||
| edit: ImageEdit, | ||
| handlers: { |
There was a problem hiding this comment.
yes the handlers are required. i thought about making them optional, and so mapImageEdit would just require edit and return, by default, edit's values in the right order.
But i'm not sure there’s anything useful to default to, and if a consumer passed one but not the other that could lead to a mistake in the final crop.
An ImageEdit is a record, so it does not carry the order its parts apply in: flip, then rotation, then crop. The order is not optional, because the rotation is measured after the flip and the crop after both. Each consumer that builds instructions for something that applies them one at a time, such as the media REST endpoint, had to know it and write it out by hand. mapImageEdit calls one handler per part, in that order, skipping the parts the edit does not have. The media editor's save uses it.
e78c16b to
5344811
Compare
andrewserong
left a comment
There was a problem hiding this comment.
Looking good to me! I think this strikes a nice balance of ensuring the order but still allowing the consumer to define the type that the mapping conforms to.
I noticed while reviewing that passing in a type seems to be required, e.g. mapImageEdit< Modifier > so I've updated the recipe to match. An alternative might be to update mapImageEdit itself to handle returning each callback the type that the function returns (so not requiring the consumer to pass in an explicit type). But, that seemed a little brittle to me as it means the consumer could basically do whatever they want with each of the callbacks, and what you've got here means that the consumer needs to go to a little bit of extra effort to ensure they all conform.
Anyway, I'm just rabbiting on here thinking out loud. I think this is a good shape, so I'll give it the ✅ and we can get this in.
Resolve the conflicts with #86704 in the README, the changelog and the package exports by keeping both sides. 0.1.0 is now published, so the applyImageEdit changelog entry moves to the new Unreleased heading, under New Features.
What?
Part of:
mapImageEdit( edit, handlers )turns each part of anImageEditinto the consumer's own format, in the order the parts apply: flip, then rotation, then crop. Parts the edit does not have are skipped. The media editor's save now uses it to build the RESTmodifiers:Why?
Follow-up to #86630, raised in review there.
ImageEditis a record, so it does not say what order its parts apply in, and the order is not optional:rotationis measured after the flip, andcropis a fraction of the image after both. Before this, every consumer that sends the edit to something that applies steps one at a time had to know that and write threeifs in the right order. The media editor did, and so did the recipe in the docs. Now the package owns the order.Testing Instructions